package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
/**
 权限管理
  \*/
@RestController
@RequestMapping("/permission")
public class PermissionController  {
    @Reference
    private PermissionService permissionService;

    //权限分页查询
    @RequestMapping("/queryPage")
    public PageResult queryPage(@RequestBody QueryPageBean queryPageBean){
       PageResult pageResult= permissionService.queryPage(queryPageBean);
       return pageResult;
    }
    //添加权限
    @RequestMapping("/addPermission")
    public Result addPermission(@RequestBody Permission permission){
        try {
            permissionService.addPermission(permission);
            return new Result(true,MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_PERMISSION_FAIL);
        }
    }
    //根据ID输出权限
    @RequestMapping("/delete")
    public Result delete(@RequestParam("id") Integer id){
        return permissionService.delete(id);
    }

    //根据权限ID查找关联的角色ID
    @RequestMapping("/findByRoleId")
    public Result findByRoleid(Integer id) {
        try {
            List<Integer> ids = permissionService.findByRoleid(id);
            return new Result(true, MessageConstant.GET_PERMISSION_LIST_SUCCESS, ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_PERMISSION_LIST_FAIL);
        }
    }
    //查询所有权限
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Permission> list = permissionService.findAll();
            return new Result(true, MessageConstant.GET_PERMISSION_LIST_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_PERMISSION_LIST_FAIL);
        }
    }
    //编辑权限
    @RequestMapping("/editPermission")
    public Result editPermission(@RequestBody Permission permission) {
        try {
            permissionService.editPermission(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }



    //根据ID查找对应的权限
    @RequestMapping("/findPermissionById")
    public Result findPermissionById(Integer id) {
        try {
            Permission permission = permissionService.findPermissionById(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }

    }
}
